﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class Main : Form
    {

        // Declare Information Object
        Information tmpInformation;

        public Main()
        {

            InitializeComponent();

        }

        private void Main_Load(object sender, EventArgs e)
        {

            // Generate Information Object
            tmpInformation = new Information(this.dataGird);

        }

        // Element : Append Button - Action : Click
        // Build : 1000 - Release Date : Oct 17th , 2013
        private void btnAppend_Click(object sender, EventArgs e)
        {

            // Get Student Information
            String tmpCode = this.txtAppendCode.Text;
            String tmpName = this.txtAppendName.Text;
            String tmpBirtday = this.dateAppendBirthday.Text;
            String tmpBirthplace = this.txtAppendBirthplace.Text;

            // Validate the Information
            if (tmpCode.Length != 7 ||
                tmpName.Length < 6 ||
                tmpBirthplace.Length < 6)
            {

                // Show the Error
                MessageBox.Show("Invalid Information !", "Student Management");

            }
            else
            {

                // Generate the Student Object
                Student tmpStudent = new Student() { Code = tmpCode, Name = tmpName, Birthday = tmpBirtday, Birthplace = tmpBirthplace };

                // Append the Student into the List
                tmpInformation.Append(tmpStudent);

                // Clear the Compornents
                txtAppendCode.Clear();
                txtAppendName.Clear();
                txtAppendBirthplace.Clear();

                // Move to the Last One
                dataGird.Rows[dataGird.RowCount - 1].Selected = true;

            }

        }

        // Element : Delete Button - Action : Click
        // Build : 1000 - Release Date : Oct 17th , 2013
        private void btnDelete_Click(object sender, EventArgs e)
        {

            // Try - Catch
            try
            {

                // Get the Code of chosen Student
                String chosenCode = dataGird.CurrentRow.Cells[0].Value.ToString();

                // Send the Command
                tmpInformation.Delete(chosenCode);

            }
            catch
            {

                // Do Nothing

            }

        }

        // Element : Save Button - Action : Click
        // Build : 1000 - Release Date : Oct 17th , 2013
        private void btnSave_Click(object sender, EventArgs e)
        {

            // Save the List
            tmpInformation.Save();

        }

        // Element : Load Button - Action : Click
        // Build : 1000 - Release Date : Oct 17th , 2013
        private void btnLoad_Click(object sender, EventArgs e)
        {

            // Load a Text File
            tmpInformation.Load();

        }

        // Element : Reorder Button - Action : Click
        // Build : 1000 - Release Date : Oct 17th , 2013
        private void btnReorder_Click(object sender, EventArgs e)
        {

            // Declare the Kind
            int tmpKind = 0;

            // Check the Radio Button
            if (rdoReorderName.Checked) { tmpKind = 1; }

            // Reorder List
            tmpInformation.Reorder(tmpKind);

        }

    }
}
